<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: wsh
 * @Date: 2024-01-31 09:52:33
 * @LastEditors: wsh
 * @LastEditTime: 2024-02-01 16:08:35
-->
<template>
  <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="0">
    <el-form-item prop="phone">
      <el-input v-model.number="ruleForm.phone" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item prop="phone">
      <el-input type="password" v-model="ruleForm.originalPassword" placeholder="请输入原密码" autocomplete="off"
        show-password />
    </el-form-item>

    <!-- <div class="flex">
			<el-form-item prop="phone" class="mr8 w-100">
				<el-input v-model="ruleForm.verificationCode" placeholder="请输入验证码"  class="w-100"/>
			</el-form-item>
			<el-button @click="handleGetCode">{{timeCount==60?"获取验证码":timeCount}}</el-button>
		</div> -->
    <el-button type="primary" @click="submitForm(ruleFormRef)" class="w-100"> 修改</el-button>
  </el-form>
</template>

<script lang="ts" setup>
import apiMod from '@/apiMod'
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { getStore } from '@/utils/store'

const ruleFormRef = ref<FormInstance>()
const userInfo = getStore('userInfo') || {}
const emit = defineEmits(['refresh'])
const checkPhone = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('请输入手机号'))
  } else {
    callback()
  }
}


const ruleForm: any = reactive({
  phone: '',
  verificationCode: '',
  originalPassword: '',
})

const rules = reactive<any>({
  phone: [{ validator: checkPhone, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      apiMod.sys_user_changePhone({ ...ruleForm, userId: userInfo.id, }).then(res => {
        ElMessage.success(res.data)
        emit('refresh')
      })
    } else {
      return false
    }
  })
}

// let timer = null
// const timeCount = ref(60)
// const handleGetCode = ()=> {
//   let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
//   if(!reg.test(ruleForm.phone)) {
//       ElMessage.warning('请输入正确的手机号!')
//       return false
//   }
//   if(timeCount.value <60) {
//     return
//   }
//   apiMod.sendVerificationCode({
//     phone:ruleForm.phone,
//     userId:userInfo.id,
//   }).then(()=> {
//    ElMessage.success('验证码已发送!')
//    timer = setInterval(()=> {
//     if(timeCount.value ==0) {
//       clearInterval(timer)
//       timeCount.value =60
//     }else {
//       timeCount.value--
//     }
//    },1000)
//   })
// }
</script>
